package com.alkin.thing.project.common;

import com.alibaba.fastjson.JSON;
import com.alkin.thing.common.utils.MyStringUtils;
import com.alkin.thing.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class CurrentUserUtils {

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     *  保存当前用户信息到redis和session中
     * @param user: 当前用户信息
     * @author: chengjie.liu 2019/7/2 11:12
     */
    public String saveUserInfo(SysUser user) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String tag = UUID.randomUUID().toString();
        request.getSession().setAttribute(Consts.TAG, tag);
        redisTemplate.opsForValue().set(tag, user.getUserName(), 30, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(tag + "userinfo", JSON.toJSONString(user),30, TimeUnit.MINUTES);

        return tag;
    }



    /**
     *  保存当前用户信息到redis和session中
     * @param user: 当前用户信息
     * @author: chengjie.liu 2019/7/2 11:12
     */
    public String saveUserInfoByToken(SysUser user, String token) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String tag = UUID.randomUUID().toString();
        request.getSession().setAttribute(Consts.TAG, tag);
        redisTemplate.opsForValue().set(tag, user.getUserName(), 30, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(tag + "userinfo", JSON.toJSONString(user),30, TimeUnit.MINUTES);

        return tag;
    }

    /**
     *  退出清空redis保存用户信息
     * @param userTag:
     * @author: chengjie.liu 2019/8/1 16:33
     */
    public void clearUserInfo (String userTag) {
        redisTemplate.delete(userTag);
        redisTemplate.delete(userTag + "userinfo");
    }

    /**
     *  获取当前用户信息
     * @author: chengjie.liu 2019/7/2 10:51
     */
    public String curUserName() throws Exception{
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object userTag = request.getSession().getAttribute(Consts.TAG);
        if (null == userTag)
            throw new RuntimeException("用户未登录");

        String tag = userTag.toString();
        if (MyStringUtils.isNotEmpty(tag) && redisTemplate.hasKey(tag)){
            String username = redisTemplate.opsForValue().get(tag);
            return username;
        }else{
            throw new RuntimeException("用户未登录");
        }
    }

    /**
     *  返回当前用户所有信息
     * @author: chengjie.liu 2019/7/23 15:26
     */
    public SysUser curUserInfo ( ){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object userTag = request.getSession().getAttribute(Consts.TAG);
        if (null == userTag)
            throw new RuntimeException("用户未登录");

        String tag = userTag.toString();
        if (MyStringUtils.isNotEmpty(tag) && redisTemplate.hasKey(tag)){
            String userInfo = redisTemplate.opsForValue().get(tag + "userinfo");
            return JSON.toJavaObject(JSON.parseObject(userInfo), SysUser.class);
        }else{
            throw new RuntimeException("用户未登录");
        }
    }

}
